--EXEC sp_create_update_inventory_transit_lot 40463,300,'ATK','FG','Admin','CG6225AAT','ATKLot3B','ATKLot3', 'ATK','0944','Running','',200,'Tape & Reel'
ALTER PROCEDURE sp_create_update_inventory_transit_lot
@LOT_ID				INT,
@QTY_IN				INT,
@NEXT_LOCATION     VARCHAR(50),
@NEXT_PROCESS      VARCHAR(50),
@USER_NAME			VARCHAR(50),
@PART_NUMBER		VARCHAR(50),
@LOT_NUMBER			VARCHAR(50),
@FAB_LOTNO			VARCHAR(50),
@VENDOR_NAME		VARCHAR(50),
@DATE_CODE			VARCHAR(50),
@LOT_STATUS			VARCHAR(50),
@LOT_COMMENTS	    VARCHAR(500),
@BULK_QTY_IN	    INT,
@CURRENT_PROCESS    VARCHAR(50)
AS 
BEGIN

	--NeED TO Implement BULK QTY IN
	DECLARE @SHIPPEDLOTID   INT
	DECLARE @DATE_OUT	DATETIME
	DECLARE @DATE_IN	DATETIME
	DECLARE @NEW_LOT_ID INT
	DECLARE @NEW_LOT_LOT_ID INT
	DECLARE @NEW_FG_LOT_ID INT
	DECLARE @NEW_LOT_FG_LOT_ID INT
	DECLARE @INVENTORY_TYPE VARCHAR(50)
	DECLARE @PROCESS_STEP_ID INT
	DECLARE @FG_PROCESSSTEP_ID INT
	DECLARE @LOCATION_ID INT
	DECLARE @FG_LOCATION_ID INT
	DECLARE @RECEIVE_VENDOR VARCHAR(50)
	DECLARE @REMAIN_QTY INT
	DECLARE @LOT_CURRENT_STATUS VARCHAR(50)
	DECLARE @QTY_IN_QTY INT

	DECLARE @LOT_PROCESSSTEP_ID INT
	DECLARE @LOT_LOCATION_ID INT
	DECLARE @SPLIT1_LOT_NO VARCHAR(50)
	DECLARE @SPLIT2_LOT_NO VARCHAR(50)
	DECLARE @LOT_CATEGORY	 VARCHAR(50)
	DECLARE @NEW_SPLIT1_LOT_ID  INT
	DECLARE @NEW_SPLIT2_LOT_ID  INT 
	DECLARE @SPLIT_SHIPPEDLOTID   INT
	DECLARE @NEW_LOT_NUMBER VARCHAR(50)
	
	SET @DATE_OUT = CONVERT(VARCHAR, CURRENT_TIMESTAMP, 101)
	
	IF @LOT_STATUS = 'HOLD'	
	BEGIN
			UPDATE LOT_TABLE SET QTY_IN = @QTY_IN, LOT_CURRENT_STATUS = 'Hold', LOT_COMMENTS = @LOT_COMMENTS,  USER_NAME = @USER_NAME WHERE LOT_ID = @LOT_ID		
			RETURN
	END

	IF @CURRENT_PROCESS <> 'FG'
	BEGIN
		--INSERTING SHIPPiNG RECORED
		INSERT INTO SHIPPED_LOT( LOT_ID, SHIP_DATE, STATUS, SHIP_QTY, TIMESTAMP, SHIP_STATUS, SL_NEXTLOCATION, SL_NEXT_PROCESS ) VALUES( @LOT_ID, @DATE_OUT, 'TRANSIT', @QTY_IN, CURRENT_TIMESTAMP, 0, @NEXT_LOCATION, @NEXT_PROCESS )
		SET @SHIPPEDLOTID = ( SELECT @@IDENTITY )
	END

	--FOR GETTING LOCATON ID SETTING THE Inventory TYPE
	IF @NEXT_PROCESS = 'FT' OR @NEXT_PROCESS = 'FPO' 
	BEGIN
			SET @INVENTORY_TYPE = 'TEST'
	END
	ELSE IF @NEXT_PROCESS = 'Tape & Reel'  
	BEGIN
			SET @INVENTORY_TYPE = 'Class Store'
	END
	ELSE IF @NEXT_PROCESS = 'FG'  
	BEGIN
			SET @INVENTORY_TYPE = 'Finished Goods'
	END
	ELSE
	BEGIN
			SET @INVENTORY_TYPE = @NEXT_PROCESS
	END
	
	Select @QTY_IN_QTY = Qty_In, @REMAIN_QTY= Qty_In-IsNULL(@QTY_IN, 0), @LOT_PROCESSSTEP_ID = PROCESSSTEP_ID, @LOT_LOCATION_ID =LOCATION_ID, @LOT_CATEGORY = LOT_CATEGORY, @DATE_IN = Date_IN  FROM LOT_Table where Lot_id = @LOT_ID
	
	SELECT @PROCESS_STEP_ID = PS.PROCESSSTEP_ID FROM PRODUCT P, PROCESS_STEP PS WHERE PS_TYPE_NAME = @INVENTORY_TYPE AND PS_TYPE = @NEXT_PROCESS AND P.PART_NUMBER = @PART_NUMBER AND P.PROCESSSTEP_ID = PS.PROCESSSTEP_ID 
	
	IF @PROCESS_STEP_ID IS NULL -- NEW PRODUCT OR PRODUCT NAME MISMATCH
	BEGIN
		SELECT TOp 1 @PROCESS_STEP_ID = PS.PROCESSSTEP_ID FROM PRODUCT_MAP_TABLE PMT,PROCESS_STEP PS WHERE PMT.PART_NUMBER = @PART_NUMBER AND PS_TYPE_NAME = @INVENTORY_TYPE AND PS_TYPE = @NEXT_PROCESS AND PMT.PROCESSSTEP_ID=PS.PROCESSSTEP_ID 
		IF @PROCESS_STEP_ID IS NULL
		BEGIN
			SELECT TOp 1 @PROCESS_STEP_ID = PROCESSSTEP_ID FROM FN_GET_PROCESS_FLOWS_BY_PROCESSSTEP_ID ( @LOT_PROCESSSTEP_ID, 1 ) WHERE PS_TYPE = @NEXT_PROCESS
			IF @PROCESS_STEP_ID IS NULL
			BEGIN	-- DUMMY UPDATE TO AVOID SQLEXCEPTION
				UPDATE LOT_TABLE SET LOT_NUMBER = 'LOT_NUMBER' WHERE LOT_NUMBER = 'LOT_NUMBER'
				RETURN
			END
		END
		ELSE
		BEGIN
			UPDATE LOT_TABLE SET LOT_NUMBER = 'LOT_NUMBER' WHERE LOT_NUMBER = 'LOT_NUMBER'
		END
	END	
	
	SELECT @LOCATION_ID = LOCATION_ID FROM LOCATION WHERE L_TYPE = @INVENTORY_TYPE AND L_VENDOR_NAME = @VENDOR_NAME
	
	

	INSERT INTO Notification_Alert_Table ( Part_Number, Lot_Number, Fab_lot_Number, Vendor_name, Process_Type, Notification_Name, Die_Qty, Ship_Qty, Status, IS_User_Notified, TimeStamp, Ship_Date )
	VALUES ( @PART_NUMBER, @LOT_NUMBER, @FAB_LOTNO, @VENDOR_NAME, @CURRENT_PROCESS, 'Yield Notification', @QTY_IN_QTY, (@QTY_IN + ISNULL(@BULK_QTY_IN, 0)), 'OPEN', 1, CURRENT_TIMESTAMP, CONVERT(VARCHAR, CURRENT_TIMESTAMP, 101) )

	IF @CURRENT_PROCESS = 'FG'
	BEGIN
		
		UPDATE LOT_TABLE SET LOT_CURRENT_STATUS = 'Close' , Lot_Split = 0, USER_NAME = @USER_NAME  WHERE LOT_ID = @LOT_ID

		IF ((SELECT COUNT(*) FROM FN_GET_PROCESS_FLOWS_BY_PROCESSSTEP_ID ( @LOT_PROCESSSTEP_ID, 1 ) ) > 1 ) --BULK FG 
		BEGIN
			SET @SPLIT1_LOT_NO = @LOT_NUMBER + 'A'			
			SET @SPLIT2_LOT_NO = @LOT_NUMBER + 'B'
		END
		ELSE
		BEGIN
			SET @SPLIT1_LOT_NO = @LOT_NUMBER + 'C'			
			SET @SPLIT2_LOT_NO = @LOT_NUMBER + 'D'
		END
		
		IF @REMAIN_QTY > 0
		BEGIN
			EXEC sp_create_update_data_entry_inventory_lot @LOT_PROCESSSTEP_ID, @LOT_LOCATION_ID, @FAB_LOTNO, @SPLIT1_LOT_NO, null/*WAFER QTY*/, null, @REMAIN_QTY, null, @DATE_IN,null,null/*vendor Name*/,null,@LOT_CATEGORY,@DATE_CODE,@LOT_STATUS,@CURRENT_PROCESS, @LOT_COMMENTS, null,@USER_NAME, -1, -1 
		END
		EXEC sp_create_update_data_entry_inventory_lot @LOT_PROCESSSTEP_ID, @LOT_LOCATION_ID, @FAB_LOTNO, @SPLIT2_LOT_NO, null/*WAFER QTY*/, null, @QTY_IN, null, @DATE_IN,null,null/*vendor Name*/,null,@LOT_CATEGORY,@DATE_CODE,@LOT_STATUS,@NEXT_PROCESS, @LOT_COMMENTS, null,@USER_NAME, -1, -1 
		
		--HERE @SPLITTWO_LOT_NO IS ALWAYS TRANSFERED TO TAPE & RELL
		SELECT @NEW_SPLIT2_LOT_ID = LOT_ID FROM LOT_TABLE WHERE PROCESSSTEP_ID = @LOT_PROCESSSTEP_ID AND LOCATION_ID = @LOT_LOCATION_ID AND LOT_NUMBER = @SPLIT2_LOT_NO  

		UPDATE LOT_TABLE SET LOT_CURRENT_STATUS = 'Close', Qty_Out = @QTY_IN, DATE_OUT = @DATE_OUT WHERE LOT_ID = @NEW_SPLIT2_LOT_ID
		
		INSERT INTO SHIPPED_LOT( LOT_ID, SHIP_DATE, STATUS, SHIP_QTY, TIMESTAMP, SHIP_STATUS, SL_NEXTLOCATION, SL_NEXT_PROCESS ) VALUES( @NEW_SPLIT2_LOT_ID, @DATE_OUT, 'TRANSIT', @QTY_IN, CURRENT_TIMESTAMP, 0, @NEXT_LOCATION, @NEXT_PROCESS )	
		SET @SPLIT_SHIPPEDLOTID = ( SELECT @@IDENTITY )	

		--SELECT @NEW_SPLIT1_LOT_ID = LOT_ID FROM LOT_TABLE WHERE PROCESSSTEP_ID = @LOT_PROCESSSTEP_ID AND LOCATION_ID = @LOT_LOCATION_ID AND LOT_NUMBER = @SPLIT1_LOT_NO  
		--IF @REMAIN_QTY = 0
		--BEGIN
		--	UPDATE LOT_TABLE SET LOT_CURRENT_STATUS = 'Close' WHERE LOT_ID = @NEW_SPLIT1_LOT_ID
		--END
		/*UPDATE LOT_TABLE SET QTY_IN = QTY_IN-IsNULL(@QTY_IN, 0), LOT_DATE_CODE = @DATE_CODE, LOT_COMMENTS = @LOT_COMMENTS, USER_NAME = @USER_NAME WHERE LOT_ID = @LOT_ID
		UPDATE INVENTORY_LOT SET IL_QTY_IN = IL_QTY_IN-ISNULL(@QTY_IN, 0), IL_QTY_BALANCE = IL_QTY_BALANCE-ISNULL(@QTY_IN, 0) , TIMESTAMP = CURRENT_TIMESTAMP WHERE LOT_ID = @LOT_ID

		SELECT @REMAIN_QTY = ISNULL(QTY_IN, 0) FROM LOT_TABLE WHERE LOT_ID = @LOT_ID
		IF @REMAIN_QTY = 0
		BEGIN
			UPDATE LOT_TABLE SET LOT_CURRENT_STATUS = 'Close' WHERE LOT_ID = @LOT_ID
		END*/
	END
	ELSE
	BEGIN
		UPDATE LOT_TABLE SET QTY_OUT = @QTY_IN + ISNULL(@BULK_QTY_IN, 0), DATE_OUT = @DATE_OUT, LOT_DATE_CODE = @DATE_CODE, LOT_CURRENT_STATUS = 'Close', LOT_COMMENTS = @LOT_COMMENTS, USER_NAME = @USER_NAME WHERE LOT_ID = @LOT_ID		
	END
	
	--WHEN BULK QTY IS NULL 
	IF @BULK_QTY_IN IS NOT NULL AND @BULK_QTY_IN > 0
	BEGIN
			--Just inserting the transit recored with close status
			INSERT INTO SHIPPED_LOT( LOT_ID, SHIP_DATE, STATUS, SHIP_QTY, TIMESTAMP, SHIP_STATUS, SL_NEXTLOCATION, SL_NEXT_PROCESS,Close_Date, Transfer_To_Bulk ) VALUES( @LOT_ID, @DATE_OUT, 'Close', @BULK_QTY_IN, CURRENT_TIMESTAMP, 1, @NEXT_LOCATION, @NEXT_PROCESS, CURRENT_TIMESTAMP, 1 )

			SELECT TOP 1 @FG_PROCESSSTEP_ID = PROCESSSTEP_ID FROM FN_GET_PROCESS_FLOWS_BY_PROCESSSTEP_ID( @PROCESS_STEP_ID,0 )  WHERE PS_TYPE = @NEXT_PROCESS AND Product_name != @PART_NUMBER
			SELECT @FG_LOCATION_ID = LOCATION_ID FROM LOCATION WHERE L_TYPE = @INVENTORY_TYPE AND L_VENDOR_NAME = @NEXT_LOCATION
			
			SELECT @NEW_FG_LOT_ID = LOT_ID FROM LOT_TABLE WHERE PROCESSSTEP_ID = @FG_PROCESSSTEP_ID AND LOCATION_ID = @FG_LOCATION_ID AND LOT_NUMBER = @LOT_NUMBER 
			
			IF @NEW_FG_LOT_ID IS NULL
			BEGIN								
					insert into Lot_Table (ProcessStep_ID, Location_ID, Lot_Number, Fab_Lot_Number, Vendor_Lot_Number, qty_in,  Date_In, Lot_Current_Status, Timestamp,Lot_Entered_By, Lot_Category, LOT_DATE_CODE, LOT_DROP_OUT, USER_NAME) 
						values (@FG_PROCESSSTEP_ID, @FG_LOCATION_ID, @LOT_NUMBER, @FAB_LOTNO, @LOT_NUMBER,  @BULK_QTY_IN,  @DATE_OUT, 'Running', CURRENT_TIMESTAMP, 0, 'PROD', @DATE_CODE, 0 , @USER_NAME )
					select @NEW_FG_LOT_ID = @@IDENTITY
		
					insert into Inventory_Lot( Lot_ID, Timestamp, IL_Date_In, IL_Qty_In ) values (@NEW_FG_LOT_ID, CURRENT_TIMESTAMP, @DATE_OUT, @BULK_QTY_IN )		
			END
			ELSE 
			BEGIN
					--THIS is FOR, When DeTapeReel Lot again Moved To TRFG OR BULK FG
					SET @NEW_LOT_NUMBER = @LOT_NUMBER + 'C'
					SELECT @NEW_LOT_FG_LOT_ID = LOT_ID FROM LOT_TABLE WHERE PROCESSSTEP_ID = @FG_PROCESSSTEP_ID AND LOCATION_ID = @FG_LOCATION_ID AND LOT_NUMBER = @NEW_LOT_NUMBER
					
					IF @NEW_LOT_FG_LOT_ID IS NULL
					BEGIN					
							insert into Lot_Table (ProcessStep_ID, Location_ID, Lot_Number, Fab_Lot_Number, Vendor_Lot_Number, qty_in,  Date_In, Lot_Current_Status, Timestamp,Lot_Entered_By, Lot_Category, LOT_DATE_CODE, LOT_DROP_OUT, USER_NAME) 
								values (@FG_PROCESSSTEP_ID, @FG_LOCATION_ID, @NEW_LOT_NUMBER, @FAB_LOTNO, @NEW_LOT_NUMBER,  @BULK_QTY_IN,  @DATE_OUT, 'Running', CURRENT_TIMESTAMP, 0, 'PROD', @DATE_CODE, 0 , @USER_NAME )
							select @NEW_LOT_FG_LOT_ID = @@IDENTITY
				
							insert into Inventory_Lot( Lot_ID, Timestamp, IL_Date_In, IL_Qty_In ) values (@NEW_LOT_FG_LOT_ID, CURRENT_TIMESTAMP, @DATE_OUT, @BULK_QTY_IN )		
					END
					ELSE 
					BEGIN 
						UPDATE LOT_TABLE SET Lot_Current_Status = 'Running', QTY_IN = QTY_IN + @BULK_QTY_IN, USER_NAME = @USER_NAME WHERE LOT_ID = @NEW_FG_LOT_ID

						UPDATE Inventory_Lot SET IL_Qty_In = IL_Qty_In + @BULK_QTY_IN , IL_QTY_BALANCE = IL_QTY_BALANCE + @BULK_QTY_IN WHERE LOT_ID = @NEW_FG_LOT_ID
					END
			END
	END

	--WHEN USER TRANSFER FROM SAME VANDOR TO SAME VENDOR
	IF @VENDOR_NAME = @NEXT_LOCATION
	BEGIN
		IF @CURRENT_PROCESS = 'FG'
		BEGIN
				UPDATE SHIPPED_LOT SET STATUS = 'Close', SHIP_STATUS = 1, Close_Date = CURRENT_TIMESTAMP WHERE SHIPPEDLOT_ID = @SPLIT_SHIPPEDLOTID 
		
				SELECT @NEW_LOT_ID = LOT_ID FROM LOT_TABLE WHERE PROCESSSTEP_ID = @PROCESS_STEP_ID AND LOCATION_ID = @LOCATION_ID AND LOT_NUMBER = @SPLIT2_LOT_NO 
		END
		ELSE
		BEGIN	
				UPDATE SHIPPED_LOT SET STATUS = 'Close', SHIP_STATUS = 1, Close_Date = CURRENT_TIMESTAMP WHERE SHIPPEDLOT_ID = @SHIPPEDLOTID 
		
				SELECT @NEW_LOT_ID = LOT_ID FROM LOT_TABLE WHERE PROCESSSTEP_ID = @PROCESS_STEP_ID AND LOCATION_ID = @LOCATION_ID AND LOT_NUMBER = @LOT_NUMBER 
		END	
			
		IF @NEW_LOT_ID IS NULL
		BEGIN	
			IF @CURRENT_PROCESS = 'FG'
			BEGIN
				insert into Lot_Table (ProcessStep_ID, Location_ID, Lot_Number, Fab_Lot_Number, Vendor_Lot_Number, qty_in,  Date_In, Lot_Current_Status, Timestamp,Lot_Entered_By, Lot_Category, LOT_DATE_CODE, LOT_DROP_OUT, USER_NAME) 
					values (@PROCESS_STEP_ID, @LOCATION_ID, @SPLIT2_LOT_NO, @FAB_LOTNO, @SPLIT2_LOT_NO,  @QTY_IN,  @DATE_OUT, 'Running', CURRENT_TIMESTAMP, 0, 'PROD', @DATE_CODE, 0 , @USER_NAME )
				select @NEW_LOT_ID = @@IDENTITY
			END
			ELSE
			BEGIN
			
				insert into Lot_Table (ProcessStep_ID, Location_ID, Lot_Number, Fab_Lot_Number, Vendor_Lot_Number, qty_in,  Date_In, Lot_Current_Status, Timestamp,Lot_Entered_By, Lot_Category, LOT_DATE_CODE, LOT_DROP_OUT, USER_NAME) 
					values (@PROCESS_STEP_ID, @LOCATION_ID, @LOT_NUMBER, @FAB_LOTNO, @LOT_NUMBER,  @QTY_IN,  @DATE_OUT, 'Running', CURRENT_TIMESTAMP, 0, 'PROD', @DATE_CODE, 0 , @USER_NAME )
				select @NEW_LOT_ID = @@IDENTITY
			END

				
			IF @INVENTORY_TYPE = 'Die Bank' OR @INVENTORY_TYPE = 'Class Store' OR @INVENTORY_TYPE = 'Finished Goods'
			BEGIN
				--select * from Inventory_Lot
				insert into Inventory_Lot( Lot_ID, Timestamp, IL_Date_In, IL_Qty_In ) values (@NEW_LOT_ID, CURRENT_TIMESTAMP, @DATE_OUT, @QTY_IN )		
			END
			ELSE IF @INVENTORY_TYPE = 'Assembly' 
			BEGIN
				--select * from Assembly_Lot
				insert into Assembly_Lot( Lot_ID, Timestamp ) values (@NEW_LOT_ID, CURRENT_TIMESTAMP)		
			END
			ELSE IF @INVENTORY_TYPE = 'TEST' 
			BEGIN
				--select * from Final_Test_Lot
				insert into Final_Test_Lot( Lot_ID, Timestamp ) values (@NEW_LOT_ID, CURRENT_TIMESTAMP)		
			END
				
		END
		ELSE 
		BEGIN
			--THIS is FOR, When DeTapeReel Lot again Moved To TRFG OR BULK FG

			IF @CURRENT_PROCESS = 'FG'
			BEGIN
				SET @NEW_LOT_NUMBER = @LOT_NUMBER + 'D'
			
				SELECT @NEW_LOT_LOT_ID = LOT_ID FROM LOT_TABLE WHERE PROCESSSTEP_ID = @PROCESS_STEP_ID AND LOCATION_ID = @LOCATION_ID AND LOT_NUMBER = @NEW_LOT_NUMBER 
			
				IF @NEW_LOT_LOT_ID IS NULL
				BEGIN
					insert into Lot_Table (ProcessStep_ID, Location_ID, Lot_Number, Fab_Lot_Number, Vendor_Lot_Number, qty_in,  Date_In, Lot_Current_Status, Timestamp,Lot_Entered_By, Lot_Category, LOT_DATE_CODE, LOT_DROP_OUT, USER_NAME) 
						values (@PROCESS_STEP_ID, @LOCATION_ID, @NEW_LOT_NUMBER, @FAB_LOTNO, @NEW_LOT_NUMBER,  @QTY_IN,  @DATE_OUT, 'Running', CURRENT_TIMESTAMP, 0, 'PROD', @DATE_CODE, 0 , @USER_NAME )
					select @NEW_LOT_LOT_ID = @@IDENTITY
				
					insert into Inventory_Lot( Lot_ID, Timestamp, IL_Date_In, IL_Qty_In ) values (@NEW_LOT_LOT_ID, CURRENT_TIMESTAMP, @DATE_OUT, @QTY_IN )		
				END
				ELSE
				BEGIN
					IF @LOT_CURRENT_STATUS <> 'Close'
					BEGIN
							UPDATE LOT_TABLE SET QTY_IN = QTY_IN + @QTY_IN, USER_NAME = @USER_NAME WHERE LOT_ID = @NEW_LOT_ID
							UPDATE Inventory_Lot SET IL_Qty_In = IL_Qty_In + @QTY_IN , IL_QTY_BALANCE = IL_QTY_BALANCE + @QTY_IN WHERE LOT_ID = @NEW_LOT_ID
					END
					ELSE
					BEGIN
							UPDATE LOT_TABLE SET Lot_Current_Status = 'Running', QTY_IN = @QTY_IN, USER_NAME = @USER_NAME WHERE LOT_ID = @NEW_LOT_ID
							UPDATE Inventory_Lot SET IL_Qty_In = @QTY_IN ,IL_QTY_BALANCE = IL_QTY_BALANCE + @QTY_IN WHERE LOT_ID = @NEW_LOT_ID
					END
				END
			END
			ELSE
			BEGIN
				SELECT @LOT_CURRENT_STATUS = LOT_CURRENT_STATUS FROM LOT_TABLE WHERE LOT_ID = @NEW_LOT_ID
				IF @LOT_CURRENT_STATUS <> 'Close'
				BEGIN
						UPDATE LOT_TABLE SET QTY_IN = QTY_IN + @QTY_IN, USER_NAME = @USER_NAME WHERE LOT_ID = @NEW_LOT_ID
						IF @INVENTORY_TYPE = 'Die Bank' OR @INVENTORY_TYPE = 'Class Store' OR @INVENTORY_TYPE = 'Finished Goods'
						BEGIN
							UPDATE Inventory_Lot SET IL_Qty_In = IL_Qty_In + @QTY_IN , IL_QTY_BALANCE = IL_QTY_BALANCE + @QTY_IN WHERE LOT_ID = @NEW_LOT_ID
						END
				END
				ELSE
				BEGIN
						UPDATE LOT_TABLE SET Lot_Current_Status = 'Running', QTY_IN = @QTY_IN, USER_NAME = @USER_NAME WHERE LOT_ID = @NEW_LOT_ID
						IF @INVENTORY_TYPE = 'Die Bank' OR @INVENTORY_TYPE = 'Class Store' OR @INVENTORY_TYPE = 'Finished Goods'
						BEGIN
							UPDATE Inventory_Lot SET IL_Qty_In = @QTY_IN ,IL_QTY_BALANCE = IL_QTY_BALANCE + @QTY_IN WHERE LOT_ID = @NEW_LOT_ID
						END
				END
			END
		END
	END
	
END